// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const savedvoiceCollection = db.collection('savedvoice')


// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const voiceId = event.voiceId
  const state = event.state || ''
  let message = 0
  let img = ''
  await savedvoiceCollection.doc(voiceId).get().then(res => {
    img = res.data.coverImg
  })
  if (img != '' && state != 'notDeleteImg') {
    await cloud.deleteFile({
      fileList: [img],
    })
  }

  await savedvoiceCollection.doc(voiceId).remove().then(res => {
    message = res.stats.removed
  })

  return message
}